
from django.shortcuts import render
from rest_framework.mixins import ListModelMixin,CreateModelMixin,UpdateModelMixin,DestroyModelMixin
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from api.models import User,Employee
from utils.response import APIResponse
from api.serializers import UserModelSerializer,EmployeerModelSerializer
# Create your views here.

#处理注册请求
class RUserAPIAView(APIView):

    def post(self,request,*args,**kwargs):
        """
        前段用户注册请求的处理
        :param request:  前段发送的用户提交的注册信息
        :param args:
        :param kwargs:
        :return:         注册成功与否,返回信息
        """
        request_data=request.data
        print(request_data)
        print(11111)
        #使用校验规则
        userserializer=UserModelSerializer(data=request_data,context={"request":request})   #由于返回的是单个,所以后面不用加many=True
                                                                    #context用于将view函数里面的参数传递给此序列化器
        userserializer.is_valid(raise_exception=True)           #进行校验
        user_obj=userserializer.save()
        print(user_obj)#然后返回一个值
        if request_data:  #如果存在即成功
            return APIResponse({
                "status":200,
                "message":True,
                "result":UserModelSerializer(user_obj).data
            })
        return APIResponse({  #否则失败
            "status": 400,
            "message": False,
        })

#处理登录请求
class LUserAPIView(APIView):
    def post(self,request,*args,**kwargs):
        """
        处理登录请求,验证用户名和密码 是否正确
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        request_data=request.data
#用户名和密码的验证就不在序列化里面写,直接写在这里
        print(request_data)
        name=request_data["l_name"]
        password=request_data["l_password"]
        user=User.objects.filter(u_name=name,password=password)
        if user:
            return APIResponse({
                "status":200,
                "message":True,
            })
        return APIResponse({
                "status":500,
                "message":False,
            })

#主页的所有信息内容显示
class EmpList(GenericAPIView,ListModelMixin):
    queryset=Employee.objects.all()
    serializer_class = EmployeerModelSerializer
    print('这是显示所有信息的主页')
    print(queryset)
    def post(self,request,*args,**kwargs):
        user_list=self.list(request,*args,**kwargs)
        print("这是即将返回的信息")
        print(user_list)
        return APIResponse({
            "status":200,
            "message":True,
            "result":user_list.data
        })
#添加逻辑
class AddList(GenericAPIView,ListModelMixin,CreateModelMixin):
    serializer_class = EmployeerModelSerializer
    def post(self,request,*args,**kwargs):
        print('添加')
        data=request.data
        print(self)
        print(request)
        e_obj=self.create(request,*args,**kwargs)
        if e_obj:
            return APIResponse({
                "status":200,
                "message":"添加信息成功",
                "result":e_obj.data
            })
        return APIResponse({
                "status":400,
                "message":"添加信息失败",
            })

#修改逻辑
class Modifylist(GenericAPIView,ListModelMixin,UpdateModelMixin):
    # id = kwargs.get("pk")
    print(id)
    lookup_field = "pk"
    queryset = Employee.objects.filter()


    serializer_class = EmployeerModelSerializer
    #获取传递过来的值,查询到所有信息,然后展示在页面上
    def post(self,request,*args,**kwargs):

        response=self.update(request,*args,**kwargs)
        return APIResponse({
            "status":200,
            "message":"修改成功",
            "result":response.data
        })




#删除,
class Deletelist(GenericAPIView,ListModelMixin,APIView):
    serializer_class = EmployeerModelSerializer
    def post(self,request,*args,**kwargs):
        print("删除")
        print(request.data)
        id=request.data["id"]
        user=Employee.objects.filter(id=id)[0]
        user.delete()
        return APIResponse({
            "status":200,
            "message":"删除一个成功",

        })



